-------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  /disk/homedirs/frenche/research/merge/impute_mcbs.log
  log type:  text
 opened on:   4 Dec 2017, 11:40:59

. set more off;

. use $raw/mergecouples.dta;

. *if death_annualize==1 we assume households dies half way between the two year period, otherwis
> e if =2 we annualize all
> dead households as if they lived for the fulll 2 year period because in HRS we do not use exact
>  death date;
. local death_annualize=2;

. ****************************************************************************************;
. * DB IMPUTATION PROCEDURE HERE                                                          ;
. ****************************************************************************************;
. ***********CURRENT MAIN JOB ************************************************************;
. * basic match criteria- keep only medicaid recipients;
. keep if medicaidind==1;
(157,028 observations deleted)

. keep if age>=65;
(19,745 observations deleted)

. * generate a HRS consistent health status - generate bad health if respondents are in;
. * fair or poor health (assign "bad" health to dead households);
. *;
. replace heal=1 if dead==1;
(1,121 real changes made)

. *generate a HRS consistent race measure;
. gen black=0;

. replace black=1 if race=="2";
(5,678 real changes made)

. *age polynomial;
. gen age2=age*age;
(3 missing values generated)

. gen age3=age2*age;
(3 missing values generated)

. gen age4=age3*age;
(3 missing values generated)

. *use oop in MCBS, which maps onto oopd in the HRS;
. *In the HRS we construct the relevant income measure during imputation;
. gen income_temp=income;
(3 missing values generated)

. gen lowlev=1000;

. replace income_temp=lowlev if income_temp<lowlev & income_temp~=.;
(502 real changes made)

. replace income_temp=log(income_temp);
(28,424 real changes made)

. *generate dummies for for the various qualifications (mirrored in HRS);
. *see Ric_1.pdf SPDEGRCV;
. gen lesshschool=0;

. replace lesshschool=inrange(education, -8,3);
(16,366 real changes made)

. gen hschool=0;

. replace hschool=1 if education==4;
(4,822 real changes made)

. gen somecollege=0;

. replace somecollege=inrange(education,5,7);
(2,409 real changes made)

. gen college=0;

. replace college=1 if education==8 | education==9;
(1,134 real changes made)

. sort married;

. by married: sum medicaid;

-------------------------------------------------------------------------------------------------
-> married = 0

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |     22,115    12267.42    20406.95          0   224836.1

-------------------------------------------------------------------------------------------------
-> married = 1

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |      6,170    8921.983    17773.24          0   211226.8

-------------------------------------------------------------------------------------------------
-> married = .

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |        139    21191.11     30155.7          0   153601.7


. by married: sum medicaid if age>75;

-------------------------------------------------------------------------------------------------
-> married = 0

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |     14,896    15139.69    21704.31          0   224836.1

-------------------------------------------------------------------------------------------------
-> married = 1

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |      3,194    12766.91    20436.22          0     109286

-------------------------------------------------------------------------------------------------
-> married = .

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |         65    31618.35     34549.7          0   153601.7


. replace married=0 if married==.;
(142 real changes made)

. replace heal=1 if heal==.;
(220 real changes made)

. replace workind=0 if workind==.;
(5,258 real changes made)

. sort married;

. by married: sum medicaid;

-------------------------------------------------------------------------------------------------
-> married = 0

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |     22,254    12323.15    20493.32          0   224836.1

-------------------------------------------------------------------------------------------------
-> married = 1

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |      6,170    8921.983    17773.24          0   211226.8


. by married: sum medicaid if age>75;

-------------------------------------------------------------------------------------------------
-> married = 0

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |     14,961    15211.28    21801.65          0   224836.1

-------------------------------------------------------------------------------------------------
-> married = 1

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |      3,194    12766.91    20436.22          0     109286


. sum medicaid age year male nurshome married heal black oop income_temp workind dead hschool som
> ecollege college age2;

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |     28,424    11584.86    19983.39          0   224836.1
         age |     28,424    79.53895    8.822687         65        114
        year |     28,427    2003.805    4.874804       1996       2012
        male |     28,427    .2819503    .4499571          0          1
    nurshome |     28,424    .3058683    .4607823          0          1
-------------+---------------------------------------------------------
     married |     28,427    .2170472    .4122423          0          1
        heal |     28,427    .5446934    .4980073          0          1
       black |     28,427    .1997397    .3998117          0          1
         oop |     28,424    3885.504    7785.747          0   299472.2
 income_temp |     28,424    9.299867    .6052852   6.907755   14.60642
-------------+---------------------------------------------------------
     workind |     28,427    .0157948    .1246832          0          1
        dead |     28,427    .1235093    .3290267          0          1
     hschool |     28,427    .1696275    .3753118          0          1
 somecollege |     28,427    .0847434    .2785043          0          1
     college |     28,427    .0398917    .1957081          0          1
-------------+---------------------------------------------------------
        age2 |     28,424    6404.281    1422.551       4225      12996

. reg medicaid age year male nurshome married heal black oop income_temp workind dead hschool som
> ecollege college age2;

      Source |       SS           df       MS      Number of obs   =    28,424
-------------+----------------------------------   F(15, 28408)    =   2493.47
       Model |  6.4508e+12        15  4.3005e+11   Prob > F        =    0.0000
    Residual |  4.8996e+12    28,408   172470813   R-squared       =    0.5683
-------------+----------------------------------   Adj R-squared   =    0.5681
       Total |  1.1350e+13    28,423   399335749   Root MSE        =     13133

------------------------------------------------------------------------------
    medicaid |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |  -532.6765    150.215    -3.55   0.000     -827.105   -238.2481
        year |   148.7023   16.51308     9.01   0.000     116.3359    181.0687
        male |  -796.4656   186.8306    -4.26   0.000    -1162.663   -430.2687
    nurshome |    34154.1   219.3552   155.70   0.000     33724.16    34584.05
     married |   833.3162   209.4653     3.98   0.000     422.7543    1243.878
        heal |   2273.412   170.3726    13.34   0.000     1939.474    2607.351
       black |   1289.918   198.1275     6.51   0.000     901.5789    1678.258
         oop |  -.1864537   .0121478   -15.35   0.000     -.210264   -.1626434
 income_temp |  -86.00235   135.4133    -0.64   0.525    -351.4188    179.4141
     workind |  -1725.507   631.9573    -2.73   0.006    -2964.173   -486.8405
        dead |  -8734.933   259.1763   -33.70   0.000    -9242.931   -8226.935
     hschool |   807.4453   216.1638     3.74   0.000      383.754    1231.137
 somecollege |   88.08647   289.3493     0.30   0.761    -479.0518    655.2247
     college |   1467.815    406.246     3.61   0.000     671.5541    2264.077
        age2 |   3.662194   .9334402     3.92   0.000     1.832607    5.491781
       _cons |  -276942.2   33597.01    -8.24   0.000    -342793.9   -211090.5
------------------------------------------------------------------------------

. gen drvisit=0;

. replace drvisit=1 if xexp_outpatient>0 &xexp_outpatient!=.;
(21,285 real changes made)

. gen hospstay=0;

. replace hospstay=1 if xexp_inpatient>0 &xexp_inpatient!=.;
(8,501 real changes made)

. gen dentist=0;

. replace dentist=1 if xexp_dental>0 &xexp_dental!=.;
(3,326 real changes made)

. gen drdenhosp=0;

. replace drdenhosp=max(drvisit,hospstay,dentist);
(23,158 real changes made)

. gen nursdead=nurshome*dead;
(3 missing values generated)

. gen nursoop=nurshome*oop;
(3 missing values generated)

. gen deadoop=dead*oop;
(3 missing values generated)

. reg medicaid age year male nurshome married heal black oop income_temp workind dead hschool som
> ecollege college age2 age3 age4 drdenhosp;

      Source |       SS           df       MS      Number of obs   =    28,424
-------------+----------------------------------   F(18, 28405)    =   2082.68
       Model |  6.4575e+12        18  3.5875e+11   Prob > F        =    0.0000
    Residual |  4.8929e+12    28,405   172253380   R-squared       =    0.5689
-------------+----------------------------------   Adj R-squared   =    0.5687
       Total |  1.1350e+13    28,423   399335749   Root MSE        =     13125

------------------------------------------------------------------------------
    medicaid |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   51028.37   15441.78     3.30   0.001     20761.75    81294.99
        year |   149.8161   16.50473     9.08   0.000     117.4661    182.1662
        male |  -776.3715   186.8203    -4.16   0.000    -1142.548   -410.1947
    nurshome |   34149.23   219.3685   155.67   0.000     33719.26    34579.21
     married |   823.2975   209.4358     3.93   0.000     412.7933    1233.802
        heal |   2235.399   171.0111    13.07   0.000     1900.209    2570.589
       black |   1262.079   198.0734     6.37   0.000     873.8452    1650.312
         oop |  -.1877516   .0121502   -15.45   0.000    -.2115666   -.1639365
 income_temp |  -81.62325    135.355    -0.60   0.546    -346.9255     183.679
     workind |   -1756.22    631.581    -2.78   0.005    -2994.149   -518.2918
        dead |  -8722.175    259.055   -33.67   0.000    -9229.935   -8214.414
     hschool |   823.3217   216.0457     3.81   0.000     399.8618    1246.782
 somecollege |   86.20213    289.184     0.30   0.766    -480.6122    653.0165
     college |   1429.109   406.0747     3.52   0.000     633.1834    2225.035
        age2 |   -903.644   280.8165    -3.22   0.001    -1454.058   -353.2303
        age3 |   7.027575   2.256184     3.11   0.002     2.605346     11.4498
        age4 |  -.0202157   .0067583    -2.99   0.003    -.0334623   -.0069691
   drdenhosp |   891.9644   201.9358     4.42   0.000     496.1607    1287.768
       _cons |   -1368150   318315.5    -4.30   0.000     -1992063   -744236.2
------------------------------------------------------------------------------

. reg medicaid age year male nurshome married heal black oop income_temp workind dead hschool som
> ecollege college age2 age3 age4 drdenhosp nursdead nursoop deadoop;

      Source |       SS           df       MS      Number of obs   =    28,424
-------------+----------------------------------   F(21, 28402)    =   1941.88
       Model |  6.6905e+12        21  3.1860e+11   Prob > F        =    0.0000
    Residual |  4.6598e+12    28,402   164066025   R-squared       =    0.5895
-------------+----------------------------------   Adj R-squared   =    0.5892
       Total |  1.1350e+13    28,423   399335749   Root MSE        =     12809

------------------------------------------------------------------------------
    medicaid |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   56384.26   15071.65     3.74   0.000     26843.11    85925.41
        year |   156.3125   16.10879     9.70   0.000     124.7385    187.8865
        male |  -937.1345   182.4115    -5.14   0.000     -1294.67   -579.5993
    nurshome |   38505.13   251.1111   153.34   0.000     38012.94    38997.32
     married |   946.4756   204.4309     4.63   0.000     545.7814     1347.17
        heal |   1758.249   167.5561    10.49   0.000     1429.831    2086.667
       black |    1248.04   193.3095     6.46   0.000     869.1443    1626.936
         oop |  -.0557365    .031414    -1.77   0.076    -.1173094    .0058364
 income_temp |  -63.39682   132.1016    -0.48   0.631    -322.3222    195.5285
     workind |  -1406.742   616.4705    -2.28   0.023    -2615.054    -198.431
        dead |  -983.7257   354.9633    -2.77   0.006    -1679.471   -287.9807
     hschool |   849.0859   210.8537     4.03   0.000     435.8027    1262.369
 somecollege |   77.72511   282.2642     0.28   0.783    -475.5262    630.9764
     college |   1425.205   396.3085     3.60   0.000     648.4216    2201.989
        age2 |  -999.5547   274.0842    -3.65   0.000    -1536.773   -462.3366
        age3 |   7.777509   2.202082     3.53   0.000     3.461324    12.09369
        age4 |  -.0223822   .0065962    -3.39   0.001    -.0353111   -.0094534
   drdenhosp |   776.5933   197.6695     3.93   0.000     389.1517    1164.035
    nursdead |  -19416.34   526.9527   -36.85   0.000     -20449.2   -18383.49
     nursoop |   -.271887   .0334868    -8.12   0.000    -.3375227   -.2062513
     deadoop |    .462827   .0347618    13.31   0.000     .3946922    .5309618
       _cons |   -1491802   310689.9    -4.80   0.000     -2100769   -882834.7
------------------------------------------------------------------------------

. gen b0 =_b[_cons];

. gen bage =_b[age];

. gen byear=_b[year];

. gen bmale=_b[male];

. gen bnurs_ind=_b[nurshome];

. gen bheal=_b[heal];

. gen bblack=_b[black];

. gen boopd=_b[oop];

. gen bfaminc_temp=_b[income_temp];

. gen blfpr=_b[workind];

. gen bdead=_b[dead];

. gen bnurs_inddead=_b[nursdead];

. gen bnurs_indoopd=_b[nursoop];

. gen bdeadoopd=_b[deadoop];

. gen bhschool =_b[hschool];

. gen bsomecollege =_b[somecollege];

. gen bcollege=_b[college];

. gen bage2=_b[age2];

. gen bage3=_b[age3];

. gen bage4=_b[age4];

. gen bmarried=_b[married];

. gen bdrdenhosp=_b[drdenhosp];

. predict medicaidp;
(option xb assumed; fitted values)
(3 missing values generated)

. sum medicaid medicaidp;

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    medicaid |     28,424    11584.86    19983.39          0   224836.1
   medicaidp |     28,424    11584.86    15342.46  -59281.75   47563.68

. drop if medicaidp==.;
(3 observations deleted)

. gen residsmt = (medicaid-medicaidp);

. preserve;

. keep b0 bage byear bmale bnurs_ind bheal bblack boopd bfaminc_temp blfpr bdead bhschool bsomeco
> llege bcollege bage2 bage3 bage4 bdrdenhosp bmarried bnurs_inddead bnurs_indoopd bdeadoopd;

. keep if _n==1;
(28,423 observations deleted)

. save $raw/impute_coeffs.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_coeffs.dta saved

. restore;

. preserve;

. keep residsmt medicaidp;

. gen donor=1;

. sort residsmt;

. save $raw/impute_donors.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_donors.dta saved

. restore;

. drop b0 bage byear bmale bnurs_ind bheal bblack boopd bfaminc_temp blfpr bdead bhschool bsomeco
> llege bcollege bage2 bage3 bage4 bdrdenhosp bmarried bnurs_inddead bnurs_indoopd bdeadoopd;

. drop residsmt;

. reg totalexp age year male nurshome married heal black oop income_temp workind dead hschool som
> ecollege college age2 age3 age4 drdenhosp nursdead nursoop deadoop;

      Source |       SS           df       MS      Number of obs   =    28,424
-------------+----------------------------------   F(21, 28402)    =   1241.94
       Model |  1.9218e+13        21  9.1515e+11   Prob > F        =    0.0000
    Residual |  2.0929e+13    28,402   736868282   R-squared       =    0.4787
-------------+----------------------------------   Adj R-squared   =    0.4783
       Total |  4.0147e+13    28,423  1.4125e+09   Root MSE        =     27145

------------------------------------------------------------------------------
    totalexp |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   31342.52   31940.84     0.98   0.326    -31263.05    93948.09
        year |   756.8414   34.13883    22.17   0.000     689.9277    823.7551
        male |   34.77699   386.5786     0.09   0.928    -722.9355    792.4895
    nurshome |   49105.01   532.1714    92.27   0.000     48061.93    50148.09
     married |    1104.47   433.2435     2.55   0.011     255.2921    1953.648
        heal |   8150.773   355.0961    22.95   0.000     7454.768    8846.778
       black |   4622.065   409.6744    11.28   0.000     3819.084    5425.046
         oop |   2.101887   .0665746    31.57   0.000     1.971397    2.232376
 income_temp |    90.9794   279.9584     0.32   0.745    -457.7525    639.7113
     workind |   -5732.96   1306.465    -4.39   0.000    -8293.695   -3172.226
        dead |   11165.42   752.2619    14.84   0.000     9690.956    12639.89
     hschool |   399.0909   446.8552     0.89   0.372    -476.7664    1274.948
 somecollege |   1275.343   598.1931     2.13   0.033      102.856     2447.83
     college |    2129.67   839.8835     2.54   0.011     483.4581    3775.881
        age2 |  -450.3567   580.8576    -0.78   0.438    -1588.865    688.1517
        age3 |   2.683286   4.666799     0.57   0.565    -6.463861    11.83043
        age4 |  -.0054182   .0139791    -0.39   0.698    -.0328178    .0219815
   drdenhosp |   12065.73   418.9144    28.80   0.000     11244.64    12886.82
    nursdead |  -27463.36   1116.753   -24.59   0.000    -29652.25   -25274.47
     nursoop |  -1.443795   .0709675   -20.34   0.000    -1.582894   -1.304695
     deadoop |   .5694959   .0736695     7.73   0.000     .4251001    .7138917
       _cons |   -2296912   658434.8    -3.49   0.000     -3587475    -1006348
------------------------------------------------------------------------------

. gen b0 =_b[_cons];

. gen bage =_b[age];

. gen byear=_b[year];

. gen bmale=_b[male];

. gen bnurs_ind=_b[nurshome];

. gen bheal=_b[heal];

. gen bblack=_b[black];

. gen boopd=_b[oop];

. gen bfaminc_temp=_b[income_temp];

. gen blfpr=_b[workind];

. gen bdead=_b[dead];

. gen bnurs_inddead=_b[nursdead];

. gen bnurs_indoopd=_b[nursoop];

. gen bdeadoopd=_b[deadoop];

. gen bhschool =_b[hschool];

. gen bsomecollege =_b[somecollege];

. gen bcollege=_b[college];

. gen bage2=_b[age2];

. gen bage3=_b[age3];

. gen bage4=_b[age4];

. gen bmarried=_b[married];

. gen bdrdenhosp=_b[drdenhosp];

. predict totalexpp;
(option xb assumed; fitted values)

. sum totalexp totalexpp;

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    totalexp |     28,424    30619.96    37582.83          0   667312.3
   totalexpp |     28,424    30619.96    26002.79  -13422.76   258915.9

. drop if totalexpp==.;
(0 observations deleted)

. gen residsmt = (totalexp-totalexpp);

. preserve;

. keep b0 bage byear bmale bnurs_ind bheal bblack boopd bfaminc_temp blfpr bdead bhschool bsomeco
> llege bcollege bage2 bage3 bage4 bdrdenhosp bmarried bnurs_inddead bnurs_indoopd bdeadoopd;

. keep if _n==1;
(28,423 observations deleted)

. save $raw/impute_totalexp_coeffs.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_totalexp_coeffs.dta saved

. restore;

. preserve;

. keep residsmt totalexpp;

. gen donor=1;

. sort residsmt;

. save $raw/impute_totalexp_donors.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_totalexp_donors.dta saved

. restore;

. drop medicaidp totalexpp residsmt b0 bage byear bmale bnurs_ind bheal bblack boopd bfaminc_temp
>  blfpr bdead bhschool bsomecollege bcollege bage2 bage3 bage4 bdrdenhosp bmarried bnurs_inddead
>  bnurs_indoopd bdeadoopd;

. destring baseid, generate(hhid) ignore(G);
baseid: character G removed; hhid generated as long

. gen check=0;

. bys hhid year :replace check=1 if baseid[_n]!=baseid[_n-1] & hhid[_n]==hhid[_n-1];
(0 real changes made)

. sum check;

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       check |     28,424           0           0          0          0

. drop check;

. xtset hhid year;
       panel variable:  hhid (unbalanced)
        time variable:  year, 1996 to 2012, but with gaps
                delta:  1 unit

. *If you are alive at the beginning of last period;
. *Annualise if you are not dead;
. *Create variables that are consistent with the HRS.;
. *All odf these variables are forwards looking!;
. gen medicaid2yr=0;

. replace medicaid2yr=(medicaid+F.medicaid)/`death_annualize' if dead==0 & F.dead==1;
(1,823 real changes made)

. replace medicaid2yr=(medicaid+F.medicaid)/2 if dead==0 & F.dead==0;
(11,326 real changes made)

. replace medicaid2yr=medicaid/`death_annualize' if dead==1;
(3,195 real changes made)

. gen oop2yr=0;

. replace oop2yr=(oop+F.oop)/`death_annualize' if dead==0 &F.dead==1;
(1,896 real changes made)

. replace oop2yr=(oop+F.oop)/2 if dead==0 &F.dead==0;
(12,337 real changes made)

. replace oop2yr=oop/`death_annualize' if dead==1;
(3,245 real changes made)

. gen nurshomedays2yr=0;

. replace nurshomedays2yr=(nurshomedays+F.nurshomedays) if dead==0 &F.dead==1;
(1,419 real changes made)

. replace nurshomedays2yr=(nurshomedays+F.nurshomedays)/2 if dead==0 &F.dead==0;
(3,975 real changes made)

. replace nurshomedays2yr=nurshomedays if dead==1;
(2,525 real changes made)

. gen nurshome2yr=0;

. replace nurshome2yr=1 if nurshomedays2yr>=60 & nurshomedays2yr!=.;
(6,537 real changes made)

. gen totalexp2yr=0;

. replace totalexp2yr=(totalexp+F.totalexp)/`death_annualize' if dead==0 &F.dead==1;
(1,926 real changes made)

. replace totalexp2yr=(totalexp+F.totalexp)/2 if dead==0 &F.dead==0;
(12,664 real changes made)

. replace totalexp2yr=totalexp/`death_annualize' if dead==1;
(3,497 real changes made)

. gen drvisit2yr=0;

. replace drvisit2yr=1 if dead==1 &drvisit==1;
(2,456 real changes made)

. replace drvisit2yr=max(drvisit,F.drvisit) if dead==0;
(20,418 real changes made)

. gen hospstay2yr=0;

. replace hospstay2yr=1 if dead==1 &hospstay==1;
(2,033 real changes made)

. replace hospstay2yr=max(hospstay,F.hospstay) if dead==0;
(8,801 real changes made)

. gen dentist2yr=0;

. replace dentist2yr=1 if dead==1 &dentist==1;
(65 real changes made)

. replace dentist2yr=max(dentist,F.dentist) if dead==0;
(4,079 real changes made)

. gen drdenhosp2yr=0;

. replace drdenhosp=max(drvisit2yr,hospstay2yr,dentist2yr);
(1,411 real changes made)

. *because created forward looking measure, create second potential year for year1 deaths;
. expand =2 if dead==1, gen(expanddummy);
(3,511 observations created)

. *update year and age for those deaths;
. replace year=year+1 if expanddummy==1;
(3,511 real changes made)

. replace age=age+1 if expanddummy==1;
(3,511 real changes made)

. replace age2=(age)^2 if expanddummy==1;
(3,511 real changes made)

. replace age3=(age)^3 if expanddummy==1;
(3,511 real changes made)

. replace age4=(age)^4 if expanddummy==1;
(3,511 real changes made)

. xtset hhid year;
       panel variable:  hhid (unbalanced)
        time variable:  year, 1996 to 2013, but with gaps
                delta:  1 unit

. *Create the (hypothetical) second year backwards looking values;
. *All of these lagged variables exist if you died because of our expand command;
. local backlist medicaid totalexp oop nurshome drdenhosp nurshomedays;

. foreach var in `backlist'{;
  2. gen `var'_tmp=L.`var'2yr;
  3. replace `var'2yr=`var'_tmp;
  4. drop `var'_tmp;
  5. };
(13,726 missing values generated)
(26,871 real changes made, 13,726 to missing)
(13,726 missing values generated)
(28,326 real changes made, 13,726 to missing)
(13,726 missing values generated)
(27,971 real changes made, 13,726 to missing)
(13,726 missing values generated)
(15,871 real changes made, 13,726 to missing)
(13,726 missing values generated)
(13,726 real changes made, 13,726 to missing)
(13,726 missing values generated)
(18,711 real changes made, 13,726 to missing)

. *Data should be backwards looking as in HRS;
. *
> 
> sum medicaid2yr age year male nurshome2yr married heal black oop2yr income_temp workind dead hs
> chool somecollege college age2 nurshomedays2yr;
. reg medicaid2yr age year male nurshome2yr married heal black oop2yr income_temp workind dead hs
> chool somecollege college age2 nurshomedays2yr;

      Source |       SS           df       MS      Number of obs   =    18,209
-------------+----------------------------------   F(16, 18192)    =   1858.12
       Model |  3.8383e+12        16  2.3989e+11   Prob > F        =    0.0000
    Residual |  2.3487e+12    18,192   129105445   R-squared       =    0.6204
-------------+----------------------------------   Adj R-squared   =    0.6200
       Total |  6.1870e+12    18,208   339794652   Root MSE        =     11362

---------------------------------------------------------------------------------
    medicaid2yr |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
----------------+----------------------------------------------------------------
            age |  -123.2389   167.0071    -0.74   0.461    -450.5886    204.1108
           year |   157.3994   18.61515     8.46   0.000      120.912    193.8869
           male |  -614.3986   204.0989    -3.01   0.003    -1014.452   -214.3455
    nurshome2yr |   6344.919   380.9802    16.65   0.000     5598.162    7091.676
        married |   842.8924   232.3243     3.63   0.000     387.5147     1298.27
           heal |   3016.705   204.9059    14.72   0.000      2615.07     3418.34
          black |   1625.869   215.2815     7.55   0.000     1203.896    2047.841
         oop2yr |  -.1676211   .0182921    -9.16   0.000    -.2034753   -.1317669
    income_temp |  -73.77799   147.3491    -0.50   0.617    -362.5961    215.0401
        workind |  -2150.491   812.7419    -2.65   0.008    -3743.542   -557.4405
           dead |  -11458.29   226.3742   -50.62   0.000       -11902   -11014.57
        hschool |   641.0689   229.7798     2.79   0.005     190.6789    1091.459
    somecollege |   246.8527   318.2351     0.78   0.438     -376.918    870.6235
        college |   1663.822   439.1398     3.79   0.000     803.0668    2524.578
           age2 |   .9160426    1.01933     0.90   0.369     -1.08194    2.914025
nurshomedays2yr |   71.33271   1.046575    68.16   0.000     69.28132    73.38409
          _cons |  -309042.2   37877.84    -8.16   0.000    -383286.3   -234798.1
---------------------------------------------------------------------------------

. gen nursdead2yr=nurshome2yr*dead;
(13,726 missing values generated)

. gen nursoop2yr=nurshome2yr*oop2yr;
(13,726 missing values generated)

. gen deadoop2yr=dead*oop2yr;
(13,726 missing values generated)

. sum medicaid2yr age year male nurshome2yr married heal black oop2yr income_temp workind dead hs
> chool somecollege college age2 age3 age4 drdenhosp2yr nursdead2yr nursoop2yr deadoop2yr nurshom
> edays2yr;

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
 medicaid2yr |     18,209    11204.25    18433.52          0   205067.9
         age |     31,935    80.18362     8.98759         65        115
        year |     31,935    2003.888    4.873432       1996       2013
        male |     31,935    .2837013    .4508006          0          1
 nurshome2yr |     18,209    .3589983    .4797199          0          1
-------------+---------------------------------------------------------
     married |     31,935    .2149679    .4108065          0          1
        heal |     31,935     .594708    .4909562          0          1
       black |     31,935    .1959605    .3969445          0          1
      oop2yr |     18,209    3421.974    5969.894          0   96390.39
 income_temp |     31,935    9.307967    .6084891   6.907755   14.60642
-------------+---------------------------------------------------------
     workind |     31,935    .0143729    .1190244          0          1
        dead |     31,935    .2198841    .4141745          0          1
     hschool |     31,935    .1708783    .3764085          0          1
 somecollege |     31,935    .0834821    .2766138          0          1
     college |     31,935    .0396117    .1950482          0          1
-------------+---------------------------------------------------------
        age2 |     31,935    6510.188    1456.937       4225      13225
        age3 |     31,935    535081.1    179361.6     274625    1520875
        age4 |     31,935    4.45e+07    1.99e+07   1.79e+07   1.75e+08
drdenhosp2yr |     18,209           0           0          0          0
 nursdead2yr |     18,209    .1737053    .3788663          0          1
-------------+---------------------------------------------------------
  nursoop2yr |     18,209    2799.799    5975.636          0   96390.39
  deadoop2yr |     18,209    1074.739    3435.589          0   89459.03
nurshomeda~r |     18,209     121.352    179.2229          0        731

. reg medicaid2yr age year male nurshome2yr married heal black oop2yr income_temp workind dead hs
> chool somecollege college age2 age3 age4 drdenhosp2yr nursdead2yr nursoop2yr deadoop2yr nurshom
> edays2yr;
note: drdenhosp2yr omitted because of collinearity

      Source |       SS           df       MS      Number of obs   =    18,209
-------------+----------------------------------   F(21, 18187)    =   1772.91
       Model |  4.1565e+12        21  1.9793e+11   Prob > F        =    0.0000
    Residual |  2.0304e+12    18,187   111642021   R-squared       =    0.6718
-------------+----------------------------------   Adj R-squared   =    0.6714
       Total |  6.1870e+12    18,208   339794652   Root MSE        =     10566

---------------------------------------------------------------------------------
    medicaid2yr |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
----------------+----------------------------------------------------------------
            age |   32080.85    16471.7     1.95   0.051    -205.2434    64366.94
           year |   166.0358   17.31404     9.59   0.000     132.0987     199.973
           male |  -897.9556   189.9642    -4.73   0.000    -1270.303   -525.6079
    nurshome2yr |   14067.89   441.0541    31.90   0.000     13203.39     14932.4
        married |   1025.779   216.2104     4.74   0.000     601.9862    1449.572
           heal |   1805.458   192.0799     9.40   0.000     1428.963    2181.953
          black |   1500.501   200.2376     7.49   0.000     1108.016    1892.985
         oop2yr |   .0489378   .0447709     1.09   0.274    -.0388173     .136693
    income_temp |   117.1013   137.0809     0.85   0.393    -151.5902    385.7927
        workind |  -1256.129   756.0183    -1.66   0.097    -2737.996    225.7385
           dead |  -1785.656   279.3337    -6.39   0.000    -2333.176   -1238.135
        hschool |   727.7958   213.7076     3.41   0.001     308.9087    1146.683
    somecollege |   284.4323   295.9867     0.96   0.337    -295.7295    864.5941
        college |   1557.111   408.4966     3.81   0.000     756.4195    2357.803
           age2 |  -569.9052   294.3317    -1.94   0.053    -1146.823    7.012699
           age3 |   4.448108   2.324309     1.91   0.056    -.1077577    9.003974
           age4 |  -.0128692   .0068454    -1.88   0.060    -.0262869    .0005485
   drdenhosp2yr |          0  (omitted)
    nursdead2yr |  -18795.76   431.9548   -43.51   0.000    -19642.44   -17949.09
     nursoop2yr |  -.3981469   .0476765    -8.35   0.000    -.4915975   -.3046964
     deadoop2yr |  -.1594075   .0367657    -4.34   0.000    -.2314718   -.0873432
nurshomedays2yr |   74.91054   1.033971    72.45   0.000     72.88386    76.93722
          _cons |   -1003033   345201.4    -2.91   0.004     -1679660   -326405.6
---------------------------------------------------------------------------------

. gen b0 =_b[_cons];

. gen bage =_b[age];

. gen byear=_b[year];

. gen bmale=_b[male];

. gen bnurs_ind=_b[nurshome2yr];

. gen bheal=_b[heal];

. gen bblack=_b[black];

. gen boopd=_b[oop2yr];

. gen bfaminc_temp=_b[income_temp];

. gen blfpr=_b[workind];

. gen bdead=_b[dead];

. gen bnurs_inddead=_b[nursdead2yr];

. gen bnurs_indoopd=_b[nursoop2yr];

. gen bdeadoopd=_b[deadoop2yr];

. gen bhschool =_b[hschool];

. gen bsomecollege =_b[somecollege];

. gen bcollege=_b[college];

. gen bage2=_b[age2];

. gen bage3=_b[age3];

. gen bage4=_b[age4];

. gen bmarried=_b[married];

. gen bdrdenhosp=_b[drdenhosp2yr];

. gen bnursing=_b[nurshomedays2yr];

. predict medicaidp;
(option xb assumed; fitted values)
(13,726 missing values generated)

. sum medicaid2yr medicaidp;

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
 medicaid2yr |     18,209    11204.25    18433.52          0   205067.9
   medicaidp |     18,209    11204.25    15108.98  -22724.71   52546.31

. *now drop the people for who medicaid2yr is missing i.e. the first wave of observation where we
>  can't construct backwards looking data;
. drop if medicaid2yr==.;
(13,726 observations deleted)

. drop if medicaidp==.;
(0 observations deleted)

. gen residsmt = (medicaid2yr-medicaidp);

. preserve;

. keep b0 bage byear bmale bnurs_ind bheal bblack boopd bfaminc_temp blfpr bdead bhschool bsomeco
> llege bcollege bage2 bage3 bage4 bdrdenhosp bmarried bnurs_inddead bnurs_indoopd bdeadoopd bnur
> sing;

. keep if _n==1;
(18,208 observations deleted)

. save $raw/impute_coeffs2yr.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_coeffs2yr.dta saved

. restore;

. preserve;

. keep residsmt medicaidp;

. gen donor=1;

. sort residsmt;

. save $raw/impute_donors2yr.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_donors2yr.dta saved

. restore;

. drop b0 bage byear bmale bnurs_ind bheal bblack boopd bfaminc_temp blfpr bdead bhschool bsomeco
> llege bcollege bage2 bage3 bage4 bdrdenhosp bmarried bnurs_inddead bnurs_indoopd bdeadoopd bnur
> sing;

. drop residsmt;

. reg totalexp2yr age year male nurshome2yr married heal black oop2yr income_temp workind dead hs
> chool somecollege college age2 age3 age4 drdenhosp2yr nursdead2yr nursoop2yr deadoop2yr nurshom
> edays2yr;
note: drdenhosp2yr omitted because of collinearity

      Source |       SS           df       MS      Number of obs   =    18,209
-------------+----------------------------------   F(21, 18187)    =    939.40
       Model |  9.1156e+12        21  4.3408e+11   Prob > F        =    0.0000
    Residual |  8.4038e+12    18,187   462078982   R-squared       =    0.5203
-------------+----------------------------------   Adj R-squared   =    0.5198
       Total |  1.7519e+13    18,208   962184708   Root MSE        =     21496

---------------------------------------------------------------------------------
    totalexp2yr |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
----------------+----------------------------------------------------------------
            age |  -23754.68   33510.64    -0.71   0.478    -89438.71    41929.34
           year |   690.2063   35.22432    19.59   0.000     621.1634    759.2493
           male |  -618.6207   386.4702    -1.60   0.109    -1376.139    138.8973
    nurshome2yr |   24323.55   897.2967    27.11   0.000     22564.76    26082.33
        married |   1166.414   439.8666     2.65   0.008     304.2345    2028.594
           heal |   7997.641   390.7744    20.47   0.000     7231.686    8763.595
          black |   4874.784   407.3708    11.97   0.000     4076.299    5673.269
         oop2yr |   2.654897   .0910836    29.15   0.000     2.476365    2.833429
    income_temp |   18.37803   278.8824     0.07   0.947    -528.2578    565.0139
        workind |  -5708.666   1538.072    -3.71   0.000    -8723.431     -2693.9
           dead |   1935.585   568.2869     3.41   0.001     821.6893    3049.481
        hschool |  -543.6461   434.7747    -1.25   0.211    -1395.846    308.5534
    somecollege |   1507.216   602.1662     2.50   0.012     326.9132    2687.519
        college |   2578.519   831.0606     3.10   0.002     949.5618    4207.476
           age2 |   481.7093   598.7993     0.80   0.421    -691.9938    1655.412
           age3 |  -4.247356   4.728661    -0.90   0.369    -13.51598    5.021266
           age4 |   .0136568   .0139266     0.98   0.327    -.0136407    .0409543
   drdenhosp2yr |          0  (omitted)
    nursdead2yr |  -26031.43   878.7848   -29.62   0.000    -27753.93   -24308.93
     nursoop2yr |  -2.090294   .0969949   -21.55   0.000    -2.280413   -1.900175
     deadoop2yr |   .1881697   .0747976     2.52   0.012     .0415594      .33478
nurshomedays2yr |   73.22209    2.10355    34.81   0.000     69.09893    77.34524
          _cons |  -944549.3   702290.6    -1.34   0.179     -2321105    432006.6
---------------------------------------------------------------------------------

. gen b0 =_b[_cons];

. gen bage =_b[age];

. gen byear=_b[year];

. gen bmale=_b[male];

. gen bnurs_ind=_b[nurshome2yr];

. gen bheal=_b[heal];

. gen bblack=_b[black];

. gen boopd=_b[oop2yr];

. gen bfaminc_temp=_b[income_temp];

. gen blfpr=_b[workind];

. gen bdead=_b[dead];

. gen bnurs_inddead=_b[nursdead2yr];

. gen bnurs_indoopd=_b[nursoop2yr];

. gen bdeadoopd=_b[deadoop2yr];

. gen bhschool =_b[hschool];

. gen bsomecollege =_b[somecollege];

. gen bcollege=_b[college];

. gen bage2=_b[age2];

. gen bage3=_b[age3];

. gen bage4=_b[age4];

. gen bmarried=_b[married];

. gen bdrdenhosp=_b[drdenhosp2yr];

. gen bnursing=_b[nurshomedays2yr];

. predict totalexpp;
(option xb assumed; fitted values)

. sum totalexp2yr totalexpp;

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
 totalexp2yr |     18,209     28505.9     31019.1          0   374216.3
   totalexpp |     18,209     28505.9    22374.96  -5724.136     142781

. drop if totalexpp==.;
(0 observations deleted)

. drop if totalexp2yr==.;
(0 observations deleted)

. gen residsmt = (totalexp2yr-totalexpp);

. preserve;

. keep b0 bage byear bmale bnurs_ind bheal bblack boopd bfaminc_temp blfpr bdead bhschool bsomeco
> llege bcollege bage2 bage3 bage4 bdrdenhosp bmarried bnurs_inddead bnurs_indoopd bdeadoopd bnur
> sing;

. keep if _n==1;
(18,208 observations deleted)

. save $raw/impute_totalexp_coeffs2yr.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_totalexp_coeffs2yr.dta saved

. restore;

. preserve;

. keep residsmt totalexpp;

. gen donor=1;

. sort residsmt;

. save $raw/impute_totalexp_donors2yr.dta , replace;
file /disk/homedirs/frenche/research/merge/impute_totalexp_donors2yr.dta saved

. log close;
      name:  <unnamed>
       log:  /disk/homedirs/frenche/research/merge/impute_mcbs.log
  log type:  text
 closed on:   4 Dec 2017, 11:42:31
-------------------------------------------------------------------------------------------------
